<?php


namespace app\unify\controller;


use app\BaseController;
use app\unify\model\Auth as AuthModel;
use think\annotation\route\Validate;
use think\db\Query;
use think\facade\Db;
use think\Paginator;
use think\Request;
use app\unify\validate\Auth as AuthValidate;

class Auth extends BaseController
{
    /**
     * 权限列表
     * @param \think\Request $r
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function index(
        string $name = null,
        string $tag = null,
        string $keywords = null
    ): Paginator
    {
        $where = [];

        if ($name) {
            $where[] = ['name', 'LIKE', "%{$name}%"];
        }

        if ($tag) {
            $where[] = ['tag', "LIKE", "%{$tag}%"];
        }

        if ($keywords) {
            $where[] = ['name|tag|description', 'LIKE', "%{$keywords}%"];
        }

        return AuthModel::where($where)->order("id DESC")->paginate($this->limit);

    }

    /**
     * 权限表单
     *
     * @param mixed $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function form(mixed $id = 0): array
    {
        $data = [
            'entity' => new \stdClass()
        ];

        if ($id) {
            $entity = AuthModel::find($id);
            $data['entity'] = $entity;
        }

        return $data;
    }

    /**
     * 新增权限
     *
     * @param \think\Request $request
     * @return string
     */
    #[Validate(AuthValidate::class, scene: AuthValidate::SAVE)]
    public function save(Request $request): string
    {

        $params = $request->param();

        AuthModel::create($params);

        return '操作成功';
    }

    /**
     * 权限信息
     *
     * @param int $id
     * @return array
     */
    public function read(int $id): array
    {
        return AuthModel::findOrEmpty($id)->toArray();
    }


    /**
     * 修改权限
     * @param \think\Request $request
     * @param int $id
     * @return string
     * @throws \Exception
     */
    #[Validate(AuthValidate::class, scene: AuthValidate::UPDATE)]
    public function update(
        int    $id,
        string $name,
        string $description,
        string $tag,
    ): string
    {

        $auth = AuthModel::findOrEmpty($id);

        if ($auth->isEmpty()) {
            throw new \Exception('未找到该权限');
        }

        $auth->save(compact('name', 'description', 'tag', 'id'));

        return '操作成功';
    }

    /**
     * 删除权限
     * @param array $ids
     * @return string
     */
    #[Validate(AuthValidate::class, scene: AuthValidate::DELETE)]
    public function delete(array $ids): string
    {
        Db::transaction(function () use ($ids) {
            AuthModel::destroy(function (Query $q) use ($ids) {
                $q->where([
                    [AuthModel::getPrimaryKey(), 'in', $ids]
                ]);
            });
        });

        return '删除成功';
    }
}